package session.stateless;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import entity.Notice;
import entity.Employee;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;


@Stateless
public class NoticeSessionBean implements NoticeSessionBeanLocal {
    
    @PersistenceContext
    private EntityManager entityManager;

    //Add Log
    @Override
    public void addNotice(String department, String description, Timestamp noticeDate, String userId, String title) {
        try {
            
            Employee employee = entityManager.find(Employee.class, userId);
            Notice notice = new Notice(department, description, noticeDate, employee, title);
            entityManager.persist(notice);
            entityManager.flush();
        } catch (Exception ex) {
            System.out.println("Exception Message: " + ex.getMessage());
        }
    }
    
    @Override
    public List<Notice> getNotices() { 
        ArrayList al = new ArrayList();

        Query query = entityManager.createQuery("SELECT n FROM Notice n ORDER BY n.noticeDate DESC");
       
        for (Object o : query.getResultList()) {
            Notice notice = (Notice) o;
            al.add(notice);
        }
        
        return al;
    }
    
 
}
